import api from '../../api/index';
import { sdk } from '../../config/sdk';

Page({
  data: {
    showpass: false
  },

  // 登录
  async login(e) {
    const { id, username, password } = e.detail.value;
    const isNumber = this.isNumber(username);
    // 是否为空
    if (!id) return this.$error('请选择学校');
    if (!username) return this.$error('学号不能为空');
    if (!password) return this.$error('密码不能为空');
    // 判断是否为数字
    if (!isNumber) return this.$error('学号必须为数字');
    wx.showLoading({ title: '登录中...', mask: true });
    const { code, message, data } = await sdk.request({ url: api.auth, data: { id, username, password } });
    wx.hideLoading();
    if (code != 1000) return this.$error(message);
    wx.navigateBack();
  },

  // 正则表达式
  isNumber(val) {
    let regPos = /^\d+(\.\d+)?$/;
    let regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
    if (regPos.test(val) || regNeg.test(val)) {
      return true;
    } else {
      return false;
    };
  },

  // 选择学校
  select() {
    const _this = this;
    wx.navigateTo({
      url: '/pages/school/index',
      events: {
        someEvent: function (data) {
          _this.setData({
            school: data
          });
        }
      }
    });
  },

  // 显示密码
  showPass() {
    var showpass = this.data.showpass;
    showpass = !showpass;
    this.setData({ showpass });
  }
})